<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Admin\ClassBaseTime;
use App\Services\Admin\Store;
use App\Services\Admin\UserSubscribeService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use phpDocumentor\Reflection\Types\Self_;

class UserSubscribeController extends Controller
{
    public function index()
    {

        $result = [
            'result' => UserSubscribeService::lists(),
            'week' => ClassBaseTime::week_result(),
            'select_store' => Store::where_lists(),
            'sear_value' => \request()->get('sear_value'),
            'week_get' => request()->get('week'),
            'store_id' => \request()->get('store_id'),
            'class_base_id' => \request()->get('class_base'),
            'classroom_id' => \request()->get('classroom'),
            'teacher_id' => \request()->get('teacher'),
            'start_time' => \request()->get('start_time'),
            'end_time' => \request()->get('end_time'),
            'is_tutoring' => \request()->get('is_tutoring'),
            'subscribe_status' => \request()->get('subscribe_status'),
            'class_base_info_id' => \request()->get('class_base_info_id'),
        ];

        return view('admin.user_subscribe.user_subscribe', $result);

    }

    public function home_index($class_base_info_id, $time)
    {
        $result = [
            'result' => UserSubscribeService::lists($class_base_info_id, $time),
            'week' => ClassBaseTime::week_result(),
            'select_store' => Store::where_lists(),
            'sear_value' => \request()->get('sear_value'),
            'week_get' => request()->get('week'),
            'store_id' => \request()->get('store_id'),
            'class_base_id' => \request()->get('class_base'),
            'classroom_id' => \request()->get('classroom'),
            'teacher_id' => \request()->get('teacher'),
            'start_time' => \request()->get('start_time'),
            'end_time' => \request()->get('end_time'),
            'is_tutoring' => \request()->get('is_tutoring'),
            'subscribe_status' => \request()->get('subscribe_status'),
            'class_base_info_id' => $class_base_info_id,
        ];

        return view('admin.user_subscribe.user_subscribe', $result);
    }

    //退出排队
    public function return_queue($id)
    {
        try {
            UserSubscribeService::return_queue($id);
        } catch (\Exception $e) {
            return self::fail($e->getMessage());
        }
        return self::success([]);
    }

    //取消预约
    public function cancel_appointment($id)
    {
        try {
            UserSubscribeService::cancel_appointment($id);
        } catch (\Exception $e) {
            return self::fail($e->getMessage());
        }
        return self::success([]);
    }

    //签到
    public function sign_in($user_subscribe_id)
    {
        try {
            UserSubscribeService::sign_in($user_subscribe_id);
        } catch (\Exception $e) {
            return self::fail($e->getMessage());
        }
        return self::success([]);
    }

    public function add()
    {

        $result = [
            'select_store' => Store::where_lists(),
            'select_id' => '',
        ];

        return view('admin.user_integral.edit', $result);
    }

    public function edit(Request $request, $id)
    {

        $info = UserIntegral::info($id);

        $result = [
            'id' => $id,
            'select_store' => Store::where_lists(),
            'select_id' => $info->store_id,
            'result' => $info,
        ];

        return view('admin.user_integral.edit', $result);

    }

    public function do(Request $request)
    {
        if ($request->ajax()) {

            $input = $request->all();
            //判断有没有过

            if (empty($input['id'])) {

                unset($input['id']);

                UserIntegral::add($input);

            } else {

                UserIntegral::edit($input['id'], $input);

            }

            return self::success([]);
        }
    }
}
